package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.TaskCommentVo;
import com.opgea.crm.common.vo.TaskVo;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.entities.ProjectDetails;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.entities.Task;
import com.opgea.crm.domain.entities.TaskComment;
import com.opgea.crm.domain.entities.security.CrmUser;
import com.opgea.crm.repository.TaskRepo;

@Service
public class TaskServiceImpl implements TaskService {

	@Autowired
	TaskRepo taskRepo;
	

	@Override
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public Long createTask(Task task) {
		return taskRepo.createTask(task);
	}

	
	public Long deleteTask(Long taskid) {
		return this.taskRepo.deleteTask(taskid);

	}

	@Override
	public List<TaskVo> tasksByProjectId(Long projectid,Long priorityId,Long assigntoId,CrmUser user) {

		List<Task> tasks = this.taskRepo.tasksByProjectId(projectid,priorityId,assigntoId,user);
		List<TaskVo> taskVos = new ArrayList<TaskVo>();
		for (Task task : tasks) {
			TaskVo vo = new TaskVo();
			vo.setActualEndDT(DateUtil.dateToString(task.getActualEndDT()));
			vo.setActualStartDT(DateUtil.dateToString(task.getActualStartDT()));
			if(task.getAssignTo()!=null){
			vo.setAssignTo(task.getAssignTo().getFirstName());
			vo.setAssignToId(task.getAssignTo().getId());
			}
			vo.setCreatedAt(DateUtil.dateToString(task.getCreatedAt()));
			if(task.getCreatedBy()!=null){
				vo.setCreatedBy(task.getCreatedBy().getUserName());
			}
			vo.setEstmDuration(task.getEstmDuration());
			vo.setDurationUnit(task.getDurationUnit().name());
			if(vo.getDurationUnit()!=null && vo.getEstmDuration()!=null){
				//vo.setDurationUnit(task.getDurationUnit().name());
				vo.setDurationWithUnit(vo.getEstmDuration()+"/"+vo.getDurationUnit());
			}
			vo.setEstmEndDate(DateUtil.dateToString(task.getEstmEndDate()));
			vo.setEstmStarDate(DateUtil.dateToString(task.getEstmStarDate()));
			vo.setId(task.getId());
			vo.setMileStone(task.isMileStone());
			if(task.getPriorityId()!=null){
				vo.setPriorityTaskId(task.getPriorityId().getId());
				vo.setPriorityTask(task.getPriorityId().getFieldValue());
			}
			if(task.getTaskResolution()!=null)
			{
				vo.setTaskResolution(task.getTaskResolution().getFieldValue());
				vo.setTaskResolutionId(task.getTaskResolution().getId());
			}
			if(task.getTaskStatus()!=null)
			{
				vo.setTaskStatus(task.getTaskStatus().getFieldValue());
				vo.setTaskStatusId(task.getTaskStatus().getId());
			}
			if(task.getProjectId()!=null)
			{
				vo.setProjectTaskName(task.getProjectId().getProjectName());
				vo.setProjectTaskId(task.getProjectId().getId());
			}
			if(task.getTaskStatus()!=null)
			{
				vo.setTaskName(task.getTaskStatus().getFieldValue());
				vo.setTaskStatusId(task.getTaskStatus().getId());
			}
			if(task.getTaskResolution()!=null)
			{
				vo.setTaskResolution(task.getTaskResolution().getFieldValue());
				vo.setTaskResolutionId(task.getTaskResolution().getId());
			}
			
			vo.setTaskName(task.getTaskName());
			vo.setTaskDetail(task.getTaskDetail());
			vo.setDueDays(DateUtil.dateDifference(task.getEstmEndDate()));
			
			
			taskVos.add(vo);
		}
		return taskVos;
	}
	
	

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void updateTask(Task task) {
		
		this.taskRepo.updateTask(task);
	}
	
	
	
		public List<SetupsValue> allProjectAndMembers(Long projectId)
		{
			List<ProjectDetails> projectDetails=this.taskRepo.allProjectAndMembers(projectId);
			List<SetupsValue> setupsValues=new ArrayList<SetupsValue>();
			for(ProjectDetails data:projectDetails)
			{
				for(Employee employee:data.getMembers())
				setupsValues.add(new SetupsValue(employee.getId(), (employee.getFirstName()+" "+employee.getLastName())));
			}
			return setupsValues;
		}
		
		public List<SetupsValue> taskUnits()
		{
			List<SetupsValue> taskList=new ArrayList<SetupsValue>();
			for(ApplicationConstant.TaskdurationUnit data:ApplicationConstant.TaskdurationUnit.values())
			
				taskList.add(new SetupsValue(Long.valueOf(data.ordinal()),data.name()));
				
		
			return taskList;
		}
		
		public List<SetupsValue> allProjects()
		{
			return this.taskRepo.allProjects();
		}
		public Task findTaskById(Long taskId)
		{
			return this.taskRepo.findTaskById(taskId);
		}


		@Override
		public List<TaskVo> tasksListByProjectId(Long projectId) {
			List<Task> tasks = this.taskRepo.tasksListByProjectId(projectId);
			List<TaskVo> taskVos = new ArrayList<TaskVo>();
			for (Task task : tasks) {
				TaskVo vo = new TaskVo();
				vo.setActualEndDT(DateUtil.dateToString(task.getActualEndDT()));
				vo.setActualStartDT(DateUtil.dateToString(task.getActualStartDT()));
				if(task.getAssignTo()!=null){
				vo.setAssignTo(task.getAssignTo().getFirstName());
				vo.setAssignToId(task.getAssignTo().getId());
				}
				vo.setCreatedAt(DateUtil.dateToString(task.getCreatedAt()));
				if(task.getCreatedBy()!=null){
				vo.setCreatedBy(task.getCreatedBy().getUserName());
				}
				vo.setEstmDuration(task.getEstmDuration());
				vo.setDurationUnit(task.getDurationUnit().name());
				if(vo.getDurationUnit()!=null && vo.getEstmDuration()!=null){
					
					vo.setDurationWithUnit(vo.getEstmDuration()+"/"+vo.getDurationUnit());
				}
				vo.setEstmEndDate(DateUtil.dateToString(task.getEstmEndDate()));
				vo.setEstmStarDate(DateUtil.dateToString(task.getEstmStarDate()));
				vo.setId(task.getId());
				vo.setMileStone(task.isMileStone());
				if(task.getPriorityId()!=null){
				vo.setPriorityTaskId(task.getPriorityId().getId());
				vo.setPriorityTask(task.getPriorityId().getFieldValue());
				}
				if(task.getTaskResolution()!=null)
				{
					vo.setTaskResolution(task.getTaskResolution().getFieldValue());
					vo.setTaskResolutionId(task.getTaskResolution().getId());
				}
				if(task.getTaskStatus()!=null)
				{
					vo.setTaskStatus(task.getTaskStatus().getFieldValue());
					vo.setTaskStatusId(task.getTaskStatus().getId());
				}
				if(task.getProjectId()!=null)
				{
					
					vo.setProjectTaskName(task.getProjectId().getProjectName());
					vo.setProjectTaskId(task.getProjectId().getId());
				}
				if(task.getTaskStatus()!=null)
				{
					vo.setTaskName(task.getTaskStatus().getFieldValue());
					vo.setTaskStatusId(task.getTaskStatus().getId());
				}
				if(task.getTaskResolution()!=null)
				{
					vo.setTaskResolution(task.getTaskResolution().getFieldValue());
					vo.setTaskResolutionId(task.getTaskResolution().getId());
				}
				
				vo.setTaskName(task.getTaskName());
				vo.setTaskDetail(task.getTaskDetail());
				vo.setDueDays(DateUtil.dateDifference(task.getEstmEndDate()));
				
				
				taskVos.add(vo);
			}
			return taskVos;
		}


	
		public List<TaskCommentVo> findAllCommentsByTaskId(long taskId) {
			List<TaskComment> commentList = taskRepo.findAllCommentsByTaskId(taskId);
			List<TaskCommentVo> commentVoList = new ArrayList<TaskCommentVo>();
			for(TaskComment comment : commentList){
				TaskCommentVo commentVo = new TaskCommentVo();
				commentVo.setTaskId(comment.getId());
				
				commentVo.setTaskUpdatedDate(DateUtil.dateToString(comment.getCreatedAt())+"/"+DateUtil.dateToTime(comment.getCreatedAt()));
				String commentString = new String(comment.getComment());
				commentVo.setTaskComment(commentString);
				if(comment.getAssignedto() != null)
				commentVo.setTaskAssignedTo(comment.getAssignedto().getFirstName());
				if(comment.getStatus() != null)
				commentVo.setTaskStatus(comment.getStatus().getFieldValue());
				commentVoList.add(commentVo);
			}
			return commentVoList;
		}
		
		
		
	
}
